package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        /// 密码进行md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
    * 新增员工功能
    *
    * */
    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        /// 用Spring提供的数据拷贝方法
        /// 可以不用再一个一个调用setter和getter方法
        /// 将employeeDTO中的数据复制到employee中

        BeanUtils.copyProperties(employeeDTO, employee);
//        1、设置账号的状态，默认正常状态
        employee.setStatus(StatusConstant.ENABLE);
//        2、设置默认密码，123456,并用md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        3、设置时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
//        4、设置用户id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.save(employee);
    }
    /**
     * 分页查询
     *
     * */
    @Override
    public PageResult pageSplit(EmployeePageQueryDTO employeePageQueryDTO) {
        Long startPage = Long.valueOf((employeePageQueryDTO.getPage() - 1)
                * employeePageQueryDTO.getPageSize());
        Long total = employeeMapper.count();
        List<Employee> records = employeeMapper.pageSplit(startPage,
                employeePageQueryDTO.getPageSize(),employeePageQueryDTO.getName());
        return new PageResult(total,records);
    }
    /**
     * 启用禁用员工账号
     *
     * */
    @Override
    public void StatusSet(Integer status, Long id) {
        Employee employee =  Employee.builder().status(status).id(id).build();

        employeeMapper.StatusSetandEdit(employee);
    }
    /**
     * 根据id查询员工信息
     *
     * */
    @Override
    public Employee getById(Long id) {
        return employeeMapper.getById(id);
    }
    /**
     * 编辑员工信息
     * */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        //设置修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //设置修改人
        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.StatusSetandEdit(employee);



    }
}
